package com.ruoyi.datam.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.base.datam.*;
import com.ruoyi.common.core.domain.business.waterDistribution.MiFarmlandB;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.zeroFill.ZeroFillUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.datam.mapper.*;
import com.ruoyi.datam.service.IWiFarmerBService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 农户信息登记Service业务层处理
 *
 * @author hwd
 * @date 2023-02-16
 */
@Service
public class WiFarmerBServiceImpl implements IWiFarmerBService {
    @Autowired
    private WiFarmerBMapper wiFarmerBMapper;
    @Autowired
    private WiOrganBMapper wiOrganBMapper;
    @Autowired
    private CmAddvBMapper cmAddvBMapper;
    @Autowired
    private MiFarmlandBMapper miFarmlandBMapper;

    /**
     * 查询农户信息登记
     *
     * @param farmCd 农户信息登记主键
     * @return 农户信息登记
     */
    @Override
    public WiFarmerB selectWiFarmerBByFarmCd(String farmCd) {
        MiFarmlandB miFarmlandB = new MiFarmlandB();
        miFarmlandB.setFarmCd(farmCd);
        miFarmlandB.setCurStatus("0");
        List<MiFarmlandB> miFarmlandBList = miFarmlandBMapper.selectMiFarmlandBList(miFarmlandB);
        String[] landCds = new String[miFarmlandBList.size()];
        for (int i = 0; i < miFarmlandBList.size(); i++) {
            landCds[i] = miFarmlandBList.get(i).getLandCd();
        }
        WiFarmerB wiFarmerB = wiFarmerBMapper.selectWiFarmerBByFarmCd(farmCd);
        wiFarmerB.setLandCd(landCds);
        return wiFarmerB;
    }

    /**
     * 查询农户信息登记列表
     *
     * @param wiFarmerB 农户信息登记
     * @return 农户信息登记
     */
    @Override
    public List<WiFarmerB> selectWiFarmerBList(WiFarmerB wiFarmerB) {
        List<WiFarmerB> wiFarmerBS = wiFarmerBMapper.selectWiFarmerBList(wiFarmerB);
        for (WiFarmerB vo : wiFarmerBS) {
            WiOrganB wiOrganB = wiOrganBMapper.selectWiOrganBByOrgCd(vo.getOrgCd());
            if (wiOrganB != null) {
                vo.setOrgNm(wiOrganB.getOrgNm());
            }
            CmAddvB cmAddvB = cmAddvBMapper.selectCmAddvBByAddvCd(vo.getAddvCd());
            if (cmAddvB != null) {
                vo.setAddvNm(cmAddvB.getAddvNm());
            }
        }
        return wiFarmerBS;
    }

    /**
     * 新增农户信息登记
     *
     * @param wiFarmerB 农户信息登记
     * @return 结果
     */
    @Override
    public int insertWiFarmerB(WiFarmerB wiFarmerB) {
        //新增後向sysUser表中新增
        List<WiFarmerB> wiFarmerBS = wiFarmerBMapper.selectWiFarmerBList(null);
        wiFarmerB.setFarmCd("FAR654322" + ZeroFillUtils.zeroFill(wiFarmerBS.size() + 1, "0000"));
        wiFarmerB.setFarmOper(SecurityUtils.getUserId() + "");
        if (wiFarmerB.getLandCd().length > 0) {
            //先刪除之前的
            miFarmlandBMapper.deleteMiFarmlandBByFarmCd(wiFarmerB.getFarmCd());
            for (String landCd : wiFarmerB.getLandCd()) {
                MiFarmlandB miFarmlandB = new MiFarmlandB();
                miFarmlandB.setFarmCd(wiFarmerB.getFarmCd());
                miFarmlandB.setLandCd(landCd);
                miFarmlandB.setRightOper(SecurityUtils.getUserId() + "");
                miFarmlandB.setRightDate(new Date());
                miFarmlandBMapper.insertMiFarmlandB(miFarmlandB);
            }
        }
        return wiFarmerBMapper.insertWiFarmerB(wiFarmerB);
    }

    /**
     * 修改农户信息登记
     *
     * @param wiFarmerB 农户信息登记
     * @return 结果
     */
    @Override
    public int updateWiFarmerB(WiFarmerB wiFarmerB) {
        if (wiFarmerB.getLandCd().length > 0) {
            //先刪除之前的
            miFarmlandBMapper.deleteMiFarmlandBByFarmCd(wiFarmerB.getFarmCd());
            for (String landCd : wiFarmerB.getLandCd()) {
                MiFarmlandB miFarmlandB = new MiFarmlandB();
                miFarmlandB.setFarmCd(wiFarmerB.getFarmCd());
                miFarmlandB.setLandCd(landCd);
                miFarmlandB.setRightOper(SecurityUtils.getUserId() + "");
                miFarmlandB.setRightDate(new Date());
                miFarmlandBMapper.insertMiFarmlandB(miFarmlandB);
            }
        }
        return wiFarmerBMapper.updateWiFarmerB(wiFarmerB);
    }

    /**
     * 批量删除农户信息登记
     *
     * @param farmCds 需要删除的农户信息登记主键
     * @return 结果
     */
    @Override
    public int deleteWiFarmerBByFarmCds(String[] farmCds) {
        return wiFarmerBMapper.deleteWiFarmerBByFarmCds(farmCds);
    }

    /**
     * 删除农户信息登记信息
     *
     * @param farmCd 农户信息登记主键
     * @return 结果
     */
    @Override
    public int deleteWiFarmerBByFarmCd(String farmCd) {
        return wiFarmerBMapper.deleteWiFarmerBByFarmCd(farmCd);
    }

    @Override
    public String checkWiFarmerNameUnique(SysUser user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        String userId2 = wiFarmerBMapper.checkWiFarmerNameUnique(user.getUserName());
        if (StringUtils.isNotBlank(userId2) && Long.parseLong(userId2) != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkWiFarmerPhoneUnique(SysUser user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        String userId2 = wiFarmerBMapper.checkWiFarmerPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotBlank(userId2) && Long.parseLong(userId2) != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertWiFarmerUser(SysUser sysUser) {
        int i = wiFarmerBMapper.insertWiFarmerUser(sysUser);
        String userId2 = wiFarmerBMapper.checkWiFarmerNameUnique(sysUser.getUserName());
        wiFarmerBMapper.insertWiFarmerUserRole(userId2,"6");
        return i;
    }

    @Override
    public String getFarmCd(String farmNm) {
        return wiFarmerBMapper.getFarmCd(farmNm);
    }


}
